package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuerry;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exxceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-16
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    //1查询讲师表所有数据

    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher()
    {
        List<EduTeacher> list = teacherService.list(null);
        try{
//        int a= 1/0;

        }catch (Exception e)
        {
            GuliException A = new GuliException(88888, "自定义异常");
            System.out.println(A.getMsg());
            throw A;
        }
        return R.ok().data("items",list);
    }


    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("delete/{id}")
    public R removeTeacher(@ApiParam(name = "id", value="讲师id",required = true) @PathVariable String id)
    {
        boolean b = teacherService.removeById(id);
        if(b)
        {
            return R.ok();
        }
        else
            {
                return R.error();
            }
    }

    @PostMapping("pageTeacher/{current}/{limit}")
    @ApiOperation("分页查询所有讲师 ")
    public R pageListTeacher(@PathVariable long current,
                                @PathVariable long limit)
    {
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        teacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        HashMap map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return  R.ok().data(map);
//        return R.ok().data("total",total).data("records",records);
    }


    //RequestBody  用post， get取不到     require =false 可以为空
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageTeacherPage/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                    @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuerry teacherQuerry
                                    )
    {
        //创建page 对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> Wrapper = new QueryWrapper<>();
        //多条件组合查询
        //动态
        String name = teacherQuerry.getName();
        Integer level = teacherQuerry.getLevel();
        String begin = teacherQuerry.getBegin();
        String end = teacherQuerry.getEnd();
        //mybatisp的动态查询
        if(!StringUtils.isEmpty(name))   //如果输入的string为空的话，不放入wrapper里面
        {
            Wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level))
        {
            Wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin))
        {
            Wrapper.gt("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end))
        {
            Wrapper.le("end",end);
        }



        //实现分页
        teacherService.page(pageTeacher,Wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        HashMap map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return  R.ok().data(map);
    }


    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher)
    {
        boolean save = teacherService.save(eduTeacher);
        if(save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }
    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacherById(@PathVariable String id)
    {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }


    @ApiOperation("讲师修改")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher)
    {
        boolean b = teacherService.updateById(eduTeacher);
        if(b)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

}

